package com.czw.ddm.register

import android.app.Application
import com.blankj.utilcode.util.AppUtils
import com.blankj.utilcode.util.DeviceUtils
import com.didi.drouter.annotation.Service
import com.mtzqc.common.BuildConfig
import com.mtzqc.common.app.App
import com.mtzqc.common.init.InitService
import com.tencent.bugly.Bugly
import com.tencent.bugly.beta.Beta
import com.tencent.bugly.crashreport.CrashReport

@Service(function = [InitService::class], priority = 100)
class BuglyRegister : InitService {

    override val depName: String
        get() = "Bugly"
    override val dependsLib: Array<Class<out InitService>>?
        get() = null
    override val depends: Array<String>?
        get() = null

    override fun initLib(app: Application, debug: Boolean, dev: Boolean, agree: Boolean): Boolean {
        //userStrategy.appChannel = BuildConfig.buildChannel //设置渠道
        //自动初始化开关
        Beta.autoInit = true
        Beta.autoCheckUpgrade = true
        Beta.enableNotification = true
        //设置开启显示打断策略
        Beta.showInterruptedStrategy = true
        //关闭热更新能力
        Beta.enableHotfix = false
        if (agree) {
            Beta.initDelay = 20 * 1000
        } else {
            Beta.initDelay = 3 * 60 * 100
        }
        Bugly.init(app, BuildConfig.buglyKey, false, CrashReport.UserStrategy(app).apply {
            appVersion = AppUtils.getAppVersionName();
            appPackageName = app.packageName
            isEnableUserInfo = true
        });

        //  userStrategy.setCrashHandleCallback(CrashHandler())
        //CrashReport.initCrashReport(app, B, debug, userStrategy)
        //设置为调试设备
        CrashReport.setIsDevelopmentDevice(app, dev)
        if (agree) {
            initOnAgree()
        }
        return true
    }

    override fun initOnAgree(): Boolean {
        // Beta.init(App.app().applicationContext,false);
        CrashReport.setDeviceId(App.app(), DeviceUtils.getUniqueDeviceId());
        return true
    }
}